﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace proyecto
{
    /// <summary>
    /// Lógica de interacción para Experiment_Design.xaml
    /// </summary>
    public partial class Experiment_Design : Window
    {
        string [] cels;
        int nCels;
        string [] classif;
        int nClassif;
        string [][] groups;
        int[] ngroups;

        int [][] celGroups;

        TreeViewItem[] nodes;
        TreeViewItem[][] children;

        bool canceled;

        

        public Experiment_Design()
        {
            InitializeComponent();
        }

        public Experiment_Design(data matrix){

            InitializeComponent();

            canceled = true;

            cels = new string[matrix.getnCols()-1];
            nCels = matrix.getnCols()-1;

            classif = new string[100];
            nClassif = 0;
            
            groups = new string[100][];
            ngroups = new int[100];

            nodes = new TreeViewItem[100];

            celGroups = new int[nCels][];

            
            children = new TreeViewItem[100][];

            for (int i=1; i<matrix.getnCols(); i++){
                ListBoxItem aux = new ListBoxItem();
                aux.Content = matrix.getColumnTittle(0, i).Replace(".cel", "");
                listBox1.Items.Add(aux);
                //listBox1.Items.Add(matrix.getColumnTittle(0,i).Replace(".cel",""));
                cels[i-1] = matrix.getColumnTittle(0,i).Replace(".cel","");
                //MessageBox.Show(cels[i]);
            }

            

        }

        public Experiment_Design(data matrix, bool designed)
        {

            InitializeComponent();

            canceled = true;

            cels = new string[matrix.getnCols() - 1];
            nCels = matrix.getnCols() - 1;

            classif = new string[100];
            nClassif = 0;

            groups = new string[100][];
            ngroups = new int[100];

            nodes = new TreeViewItem[100];

            celGroups = new int[nCels][];


            children = new TreeViewItem[100][];

            for (int i = 1; i < matrix.getnCols(); i++)
            {
                ListBoxItem aux = new ListBoxItem();
                aux.Content = matrix.getColumnTittle(0, i).Replace(".cel", "");
                listBox1.Items.Add(aux);
                //listBox1.Items.Add(matrix.getColumnTittle(0,i).Replace(".cel",""));
                cels[i - 1] = matrix.getColumnTittle(0, i).Replace(".cel", "");
                //MessageBox.Show(cels[i]);
            }

            /*for (int i = 0; i<matrix.nClass; i++){
                treeView1.Items.Add(matrix.gr
                for (int j = 0; j<ngroups[i]; j++){*/

        }


        //CREATE EF
        private void button5_Click(object sender, RoutedEventArgs e)
        {
            nodes[nClassif] = new TreeViewItem();
            nodes[nClassif].Header = textBox1.Text;
            nodes[nClassif].IsSelected = true;
            treeView1.Items.Add(nodes[nClassif]);
            classif[nClassif] = textBox1.Text;
            children[nClassif] = new TreeViewItem[5];
            groups[nClassif] = new string[5];
            
            ngroups[nClassif] = 0;
            nClassif++;
            
            
        }
        //CREATE GROUP
        private void button6_Click(object sender, RoutedEventArgs e)
        {
            bool isgroup = false;

            int l;

            for (l = 0; l < nClassif && !isgroup; l++)
                for (int j = 0; j < ngroups[l] && !isgroup; j++)
                    if (groups[l][j] == (treeView1.SelectedItem as TreeViewItem).Header.ToString())
                        isgroup = true;
            l--;

            if (isgroup)
            {
                TreeViewItem newChild = new TreeViewItem();
                newChild.Header = textBox1.Text;
                groups[l][ngroups[l]] = textBox1.Text;
                ngroups[l]++;
                ((treeView1.SelectedItem as TreeViewItem).Parent as TreeViewItem).Items.Add(newChild);
            }
            else
            {
                TreeViewItem newChild = new TreeViewItem();
                newChild.Header = textBox1.Text;
                int aux;
                bool found = false;
                for (aux = 0; !found; aux++)
                    if (classif[aux] == (treeView1.SelectedItem as TreeViewItem).Header.ToString())
                        found = true;
                aux--;
                groups[aux][ngroups[aux]] = textBox1.Text;
                ngroups[aux]++;
                (treeView1.SelectedItem as TreeViewItem).Items.Add(newChild);
            }
        }
        //ADD SINGLE GENE TO GROUP
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            bool isgroup = false;

            int l;
            int j = 0; ;

            for (l = 0; l < nClassif && !isgroup; l++)
                for (j = 0; j < ngroups[l] && !isgroup; j++)
                    if (groups[l][j] == (treeView1.SelectedItem as TreeViewItem).Header.ToString())
                        isgroup = true;
            l--;
            j--;


            if (isgroup)
            {
                TreeViewItem newChild = new TreeViewItem();
                newChild.Header = (listBox1.SelectedItem as ListBoxItem).Content.ToString();
                (treeView1.SelectedItem as TreeViewItem).Items.Add(newChild);

                //celGroups[listBox1.SelectedIndex][l] = j;
            }




        }


        //add all to group
        private void button8_Click(object sender, RoutedEventArgs e)
        {
            bool isgroup = false;

            int l;
            int j = 0; ;

            for (l = 0; l < nClassif && !isgroup; l++)
                for (j = 0; j < ngroups[l] && !isgroup; j++)
                    if (groups[l][j] == (treeView1.SelectedItem as TreeViewItem).Header.ToString())
                        isgroup = true;
            l--;
            j--;


            if (isgroup)
            {
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    if ((listBox1.Items[i] as ListBoxItem).Visibility == Visibility.Visible)
                    {
                        TreeViewItem newChild = new TreeViewItem();
                        newChild.Header = (listBox1.Items[i] as ListBoxItem).Content.ToString();
                        (treeView1.SelectedItem as TreeViewItem).Items.Add(newChild);
                    }

                }

                //celGroups[listBox1.SelectedIndex][l] = j;
            }
        }
        //remove single gene from group
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            bool isGene = false;

            for (int i = 0; !isGene && i < nCels; i++)
                if ((treeView1.SelectedItem as TreeViewItem).Header.ToString() == cels[i])
                    isGene = true;

            if (isGene)
            {
                ((treeView1.SelectedItem as TreeViewItem).Parent as TreeViewItem).Items.Remove(treeView1.SelectedItem);
            }
        }
        //remove all from one group
        private void button9_Click(object sender, RoutedEventArgs e)
        {
            bool isgroup = false;

            for (int l = 0; l < nClassif && !isgroup; l++)
                for (int j = 0; j < ngroups[l] && !isgroup; j++)
                    if (groups[l][j] == (treeView1.SelectedItem as TreeViewItem).Header.ToString())
                        isgroup = true;

            while ((treeView1.SelectedItem as TreeViewItem).Items.Count > 0)
                (treeView1.SelectedItem as TreeViewItem).Items.Remove((treeView1.SelectedItem as TreeViewItem).Items[0]);


        }
        //delete group or ef
        private void button7_Click(object sender, RoutedEventArgs e)
        {
            bool isgroup = false;

            int l;
            int j = 0; ;

            for (l = 0; l < nClassif && !isgroup; l++)
                for (j = 0; j < ngroups[l] && !isgroup; j++)
                    if (groups[l][j] == (treeView1.SelectedItem as TreeViewItem).Header.ToString())
                        isgroup = true;
            l--;
            j--;

            if (isgroup)
            {
                deleteGroup(l, j);
                ((treeView1.SelectedItem as TreeViewItem).Parent as TreeViewItem).Items.Remove(treeView1.SelectedItem);
            }

            else
            {

                for (l = 0; l < nClassif; l++)
                    if (classif[l] == (treeView1.SelectedItem as TreeViewItem).Header.ToString())
                        isgroup = true;

                if (isgroup)
                {
                    deleteClassif(l);
                    treeView1.Items.Remove(treeView1.SelectedItem);
                }
            }
        }

        public void deleteGroup(int i, int j)
        {
            ngroups[i]--;
            for (int l = j; l < ngroups[i]; l++)
                groups[i][l] = groups[i][l + 1];
        }

        public void deleteClassif(int i)
        {
            nClassif--;
            for (int j = i; j < nClassif; j++)
                classif[j] = classif[j + 1];
        }

        //load design from file
        private void button10_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog design = new OpenFileDialog();
            design.Title = "Load experiment design file";
            design.ShowDialog();

            string e_design = design.FileName;

            StreamReader reader = new StreamReader(e_design);

            string aux;

            aux = reader.ReadLine().Replace("/", "_").Replace("-", "_").Replace("\\", "_");

            string[] aux1 = aux.Split('\t', ' ', ';', ',');

            for (int i = 1; i < aux1.Length; i++)
            {
                classif[i - 1] = aux1[i];
                nodes[i-1] = new TreeViewItem();
                nodes[i-1].Header = aux1[i];
                nodes[i-1].IsSelected = true;
                nodes[i - 1].IsExpanded = true;
                treeView1.Items.Add(nodes[i-1]);
                children[nClassif] = new TreeViewItem[5];
                groups[nClassif] = new string[5];

                ngroups[nClassif] = 0;
                nClassif++;

                
            }
            MessageBox.Show("File loaded succesfully");



            aux = reader.ReadToEnd().Replace("/", "_").Replace("-", "_").Replace("\\", "_");

            string[] aux2 = aux.Split('\n');


            string[][] aux3 = new string[nCels][];

            for (int i = 0; i < nCels; i++)
            {
                aux2[i] = aux2[i].Trim();
                aux3[i] = aux2[i].Split('\t', ' ', ';', ',');
            }


            for (int i = 0; i<nCels; i++)
                for (int j = 0; j < nClassif; j++)
                {
                    bool different = true;
                    bool isgroup = false;
                    for (int c = 0; c < nClassif && different; c++)
                    {
                        if (aux3[i][j + 1] == classif[c])
                            different = false;
                        else
                            for (int g = 0; g < ngroups[c] && different; g++)
                                if (aux3[i][j + 1] == groups[c][g])
                                {
                                    different = false;
                                    isgroup = true;
                                    //MessageBox.Show((treeView1.Items[c] as TreeViewItem).Header.ToString() + " - " + ((treeView1.Items[c] as TreeViewItem).Items[g] as TreeViewItem).Header.ToString());
                                    ((treeView1.Items[c] as TreeViewItem).Items[g] as TreeViewItem).IsSelected = true;
                                }
                    }

                    if (different)
                    {
                        groups[j][ngroups[j]] = aux3[i][j + 1];
                        ngroups[j]++;
                        TreeViewItem newChild = new TreeViewItem();
                        newChild.Header = aux3[i][j + 1];
                        //(treeView1.Items[j] as TreeViewItem).IsExpanded = true;
                        newChild.IsSelected = true;
                        (treeView1.Items[j] as TreeViewItem).Items.Add(newChild);
                        isgroup = true;
                    }

                    if (isgroup)
                    {
                        TreeViewItem newChild = new TreeViewItem();
                        newChild.Header = aux3[i][0];
                        newChild.IsSelected = true;
                        (treeView1.SelectedItem as TreeViewItem).Items.Add(newChild);
                    }
                }
                        


            /*for (int i = 1; i <= nClassif; i++)
            {
                
                
                for (int j = 0; j < nCels; j++)
                {
                    bool different = true;
                    bool isgroup = false;
                    for (int l = 0; l<nClassif&&different; l++){
                        if (aux3[j][i]==classif[l])
                            different = false;
                        else 
                            for (int k=0; k<ngroups[l]; k++)
                                if (aux3[j][i]== groups[l][k]){
                                    different = false;
                                    isgroup = true;
                                    (treeView1.Items[l] as TreeViewItem).IsSelected = true;
                                }
                    }

                    if (different) isgroup = true;


                    if (different)
                    {
                        groups[i-1][ngroups[i-1]]=aux3[j][i];
                        ngroups[i-1]++;
                        TreeViewItem newChild = new TreeViewItem();
                        newChild.Header = aux3[j][i];    
                        newChild.IsSelected = true;
                        (treeView1.Items[i-1] as TreeViewItem).Items.Add(newChild);

                        //MessageBox.Show(aux3[j][i]);
                        

                        
                        
                    }
                    if (isgroup)
                    {
                        
                        if (!different)
                        {
                            ((treeView1.SelectedItem as TreeViewItem).Items[(treeView1.SelectedItem as TreeViewItem).Items.Count-1] as TreeViewItem).IsSelected = true;

                            int l, k;
                            bool selected = false;
                            for (l = 0; l < nClassif && !selected; l++)
                                for (k = 0; k < ngroups[k] && !selected; k++)
                                    if (groups[l][k] == aux3[j][i])
                                    {

                                        ((treeView1.Items[l] as TreeViewItem).Items[k] as TreeViewItem).IsSelected = true;
                                        selected = true;
                                        //MessageBox.Show(groups[l][k] + " = " + aux3[j][i] + " = " + (treeView1.SelectedItem as TreeViewItem).Header.ToString());
                                    }
                        }

                        TreeViewItem newChild = new TreeViewItem();
                        newChild.Header = aux3[j][0];
                        (treeView1.SelectedItem as TreeViewItem).Items.Add(newChild);
                        
                    }



                }
                
            }*/

            

        }


        //OK button
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i< nCels; i++)
                celGroups[i] = new int[nClassif];

            

            for (int i = 0; i < nClassif; i++)
            {

                //MessageBox.Show("Classificación " + i.ToString() + " : " + classif[i] + " con " + ngroups[i].ToString());

                for (int j = 0; j < ngroups[i]; j++)
                {
                    //MessageBox.Show("Group " + j.ToString() + ": " + groups[i][j]);

                    for (int k = 0; k < ((treeView1.Items[i] as TreeViewItem).Items[j] as TreeViewItem).Items.Count; k++)
                    {
                        int l;
                        string group = ((treeView1.Items[i] as TreeViewItem).Items[j] as TreeViewItem).Header.ToString();

                        bool found = false;

                        for (l = 0; !found; l++)
                            if (cels[l] == (((treeView1.Items[i] as TreeViewItem).Items[j] as TreeViewItem).Items[k] as TreeViewItem).Header.ToString())
                            {

                                found = true;
                            }
                        l--;

                        celGroups[l][i] = j;

                        //MessageBox.Show(cels[l] + " - " + i.ToString() + classif[i] + ": " + j.ToString());
                    }
                }

            }
            canceled = false;
            DialogResult = true;
            this.Close();
        }


        //Save to file
        private void button11_Click(object sender, RoutedEventArgs e)
        {

        }


        //cancel button
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        public int getnClassif()
        {
            return nClassif;
        }

        public int getnGroups(int i)
        {
            return ngroups[i];
        }

        public string getgroupLabel(int i, int j)
        {
            return ((treeView1.Items[i] as TreeViewItem).Items[j] as TreeViewItem).Header.ToString();
        }

        public int getGroup(int i, int j)
        {
            return celGroups[i][j];
        }

        public string getClassLabel(int i)
        {
            return (treeView1.Items[i] as TreeViewItem).Header.ToString();
        }

        private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
        {
            for (int i = 0; i < listBox1.Items.Count; i++)
                if (listBox1.Items[i].ToString().Contains(textBox2.Text))
                    (listBox1.Items[i] as ListBoxItem).Visibility = Visibility.Visible;
                else
                    (listBox1.Items[i] as ListBoxItem).Visibility = Visibility.Hidden;
        }

        public bool result()
        {
            return !canceled;
        }



    }
}
